﻿#pragma once
namespace LK
{
	//默认的代理类实现（默认从ptr<Object>派生）
	template<class T>struct ptr :UseT<!IsBaseOf<Object, T>::V, ptr<Object> >::T { PIMETHODS(ptr, T)template<class OT>inline ptr(OT const&t, typename UseIf<IsBaseOf<T, typename UseT<IsBaseOf<PObject, OT>::V, PObject, OT>::T::IObj>::V>::T*p = NULL) { ptr<Object>::operator=(t); } };

	template<class TT>struct TypeOfReference {
		typedef typename UseT<IsBaseOf<ptr<Object>, TT >::V, ptr<Object>, TT>::T TEST;
		typedef typename TEST::IObj T;
	};


#ifdef LK_WEAK_
    template<class T>weak<T>::weak() {}
    template<class T>weak<T>::weak(null_t) {}
    template<class T>weak<T>::weak(typename TypeOfReference<T>::T const* p) :T(){ ptr<Object>::pObject = (typename TypeOfReference<T>::T*)p; if (p) { p->UseCount.Create(); T::UseCount = p->UseCount; } }
    template<class T>weak<T>::weak(const T& w) :T() { T::UseCount = w.UseCount; ptr<Object>::pObject = ((ptr<Object>&)w).pObject; }
    template<class T>weak<T>::weak(const weak&w) :T() { T::UseCount = w.UseCount; ptr<Object>::pObject = ((ptr<Object>&)w).pObject; }
    template<class T>template<class AnyT>weak<T>::weak(weak<AnyT>const& w, typename UseIf<IsBaseOf<typename TypeOfReference<T>::T, typename TypeOfReference<AnyT>::T>::V>::T*p) : T() { T::UseCount = w.ptr<Object>::UseCount; ptr<Object>::pObject = ((ptr<Object>&)w).ptr<Object>::pObject; }
    template<class T>weak<T>& weak<T>::operator=(const weak&w) { T::UseCount = w.UseCount; ptr<Object>::pObject = ((ptr<Object>&)w).pObject; return *this; }
	template<class T>weak<T>& weak<T>::operator=(null_t) { SetToNull(); return *this; }
    template<class T>void weak<T>::SetToNull() { ptr<Object>::UseCount.ChangeRef(NULL); ptr<Object>::pObject = NULL; }
    template<class T>void weak<T>::SetTo(typename TypeOfReference<T>::T const*p) {if(p)ptr<Object>::UseCount=p->UseCount; ptr<Object>::pObject = (typename TypeOfReference<T>::T*)p; }
    template<class T>weak<T>::~weak() {T::UseCount.ChangeRef(NULL);}
#endif
	static void ___SwapReference(PObject& p1, PObject p2) {
		StructOfSize<sizeof(PObject)> tmp = (StructOfSize<sizeof(PObject)>&)p1;
		(StructOfSize<sizeof(PObject)>&)p1 = (StructOfSize<sizeof(PObject)>&)p2;
		(StructOfSize<sizeof(PObject)>&)p2 = tmp;
	}
	template<class T> typename UseIf<IsBaseOf<PObject, T>::V>::T Object::Swap(T& t1, T& t2){ ___SwapReference(t1, t2);}

	template<class RT,class _T,RT(_T::*fun)()>struct fref<RT(_T::*)(), fun>:PObject {
		RT operator()() { return(CastPointer<_T,Object>(((PObject*)this)->GetPtr())->*fun)(); }
		typedef RT(_T::PObj::* T)();
	};
	template<class RT,class _T,RT(_T::*fun)()const>struct fref<RT(_T::*)()const, fun>:PObject {
		RT operator()()const { return(CastPointer<_T const, Object>(((PObject*)this)->GetPtr())->*fun)(); }
		typedef RT(_T::PObj::* T)()const;
	};
#ifdef SUPPORT_VARIADIC_TEMPLATES
	template<class RT,class _T,class... Args,RT(_T::*fun)(Args...)>struct fref<RT(_T::*)(Args...), fun>:PObject {
		RT operator()(Args...args) { return(CastPointer<_T,Object>(((PObject*)this)->GetPtr())->*fun)(args...); }
		typedef RT(_T::PObj::* T)(Args...);
	};
	template<class RT,class _T,class... Args,RT(_T::*fun)(Args...)const>struct fref<RT(_T::*)(Args...)const, fun>:PObject {
		RT operator()(Args...args)const { return(CastPointer<_T const, Object>(((PObject*)this)->GetPtr())->*fun)(args...); }
		typedef RT(_T::PObj::* T)(Args...)const;
	};
#else
#define TMPF(x)template<class RT,class _T,MF3_RepeatAsc_(MF1_CT,1,x),RT(_T::*fun)(MF3_RepeatAsc_(MF1_T,1,x))>struct fref<RT(_T::*)(MF3_RepeatAsc_(MF1_T,1,x)), fun>:PObject {\
	RT operator()(MF3_RepeatAsc_(MF1_Tt,1,x)) { return(CastPointer<_T, Object>(((PObject*)this)->GetPtr())->*fun)(MF3_RepeatAsc_(MF1_t,1,x)); 	typedef RT(_T::PObj::* T)(MF3_RepeatAsc_(MF1_T,1,x));}};\
	template<class RT,class _T,MF3_RepeatAsc_(MF1_CT,1,x),RT(_T::*fun)(MF3_RepeatAsc_(MF1_T,1,x))const>struct fref<RT(_T::*)(MF3_RepeatAsc_(MF1_T,1,x))const, fun>:PObject {\
	RT operator()(MF3_RepeatAsc_(MF1_Tt,1,x))const { return(CastPointer<_T const, Object>(((PObject*)this)->GetPtr())->*fun)(MF3_RepeatAsc_(MF1_t,1,x));typedef RT(_T::PObj::* T)(MF3_RepeatAsc_(MF1_T,1,x))const;}};
	MF2_Repeatb(TMPF,16)
#undef TMPF
#endif
	template<class FunType, FunType fun>struct fref {
	};

	template<typename T>inline void ptr<Object>::template Ctor2(typename AddConst<typename AddLValueReference<T>::T>::T t)
	{
		PMObjects xr = MObjects::MakeMObjectByTypeInfo(Type::Make<T>());
		const void* p;
		(typename AddPointer<T>::T&)p= (typename AddPointer<T>::T)&t;
		if (xr->GetInnerType()->IsArray() || xr->GetInnerType()->IsLValueReference() || xr->GetInnerType()->IsFunction() != 0)//左值引用
			xr->GetInnerType()->CopyConstruct(MObjects::GetDataPtr(xr), &p);
		else xr->GetInnerType()->CopyConstruct(MObjects::GetDataPtr(xr), p);
		*this = xr;
	}
#ifdef _CLR
	template<typename T>inline void ptr<Object>::template Ctor3(T t)
	{
		PMObjects xr = MObjects::MakeMObjectByTypeInfo(Type::Make<T>());
		gcroot<T> p = t;
		xr->GetInnerType()->CopyConstruct(MObjects::GetDataPtr(xr), &(void**&)p);
		*this = xr;
	}
	template<typename T>inline void ptr<Object>::template Ctor4(T t)
	{
		PMObjects xr = MObjects::MakeMObjectByTypeInfo(Type::Make<T>());
		pin_ptr<typename RemoveReference<T>::T> p = &t;
		xr->GetInnerType()->CopyConstruct(MObjects::GetDataPtr(xr), (typename AddPointer<typename RemoveReference<T>::T>::T)p);
		*this = xr;
	}
#endif
	template<typename T>inline ptr<Object> MObjects::MakeObject(T t){
		ptr<Object> obj;
		if (IsReference<T>::V)
		{
			obj = MObjects::MakeMObjectByTypeInfo(Type::Make<T>());
			Convert<void, T>::WriteDataFrom(MObjects::GetDataPtr(obj), t);
		}
		else obj.Ctor<typename RemoveReference<T>::T>(t);
		return obj;
	}
	template <class T>T cast(PMObjects const& p){
		if (p==null)Throw< ptr<Exception> >(L"无法转换空引用");
		if (Type::Make<T>() != p->GetInnerType())
			Throw< PTypesConvertErrorException>(L"无法将类型 " + p->GetInnerType()->GetName() + L"转换到类型 " + Type::Make<T>()->GetName());
		return Convert<T, void>::FromDataPtr(p->GetDataPtr());
	}
	template <size_t size>MObject<size>::~MObject(){
		if (innerDataType!=null)
		{
			if (ptmp)
				GetInnerType()->DeconstructIgnoreReference(ptmp);
			else GetInnerType()->Deconstruct(GetDataPtr());
		}
	}
	template<class T>inline T ptr<Object>::template CastTo1()const{
		if (*this==null)return T();
		T t;
		if (Check()->GetType() == Type::Make<T>())
			((ptr<Object>&)t).SetTo(ptr<Object>::GetPtr());
		else if (Check()->GetType() == Type::Make<PMObjects>())
		{
			PObject xx = *(PObject*)MObjects::GetObjectPtr(*this);
			if (xx==null)return T();
			t = cast<T>(xx);
		}
		else//查询派生关系
		{
			if (Check()->GetType()->IsDerivedFrom(Type::Make<T>()))
				((ptr<Object>&)t).SetTo(GetPtr());
		}
		return t;
	}
	template<class T>inline T ptr<Object>::template CastTo2()const{
		if (*this==null)Throw< PPointerIsNullException >();
		PMObjects x;
		if (Check()->GetType() == Type::Make<PMObjects>())
			x = (MObjects*)GetPtr();
		return cast<T>(x);
	}
	/**
	@brief 获取类型T的ID，该方法线程安全
	@note 类型T必须是最原生类型，否则将无法编译通过*/
	template<class T>inline long Type::GetId1(){
		typedef typename ::LK::VarArgsPack<void, bool, s8, s16, i32, s32, s64, u8> BaseTypes1;
		typedef typename ::LK::VarArgsPack<u16, ui32, u32, u64, float, double, long double, char, wchar_t> BaseTypes2;
		if (ArgHasT<T, BaseTypes1>::V)return ArgHasT<T, BaseTypes1>::index + 1;
		if (ArgHasT<T, BaseTypes2>::V)return ArgHasT<T, BaseTypes2>::index + 9;
		static long index = 0;
		if (index != 0)return index;
		GetMutexRef().Lock();
		if (index == 0)
		{
			index = (long)GetLocalTypeId();
			if ((index_t)GetTypeCommonInfoRef().size() <= index)GetTypeCommonInfoRef().resize(index + 1);
		}
		GetTypeCommonInfoRef()[index].GetNameRef() = typeid(typename UseT<::LK::IsGCHandle<T>::V, T,void>::T).name();
		typedef typename UseT<::LK::IsAbstract<T>::V || IsVoid<T>::V, T, int>::T LT;
#ifdef _CLR
		typedef typename UseT < ::LK::IsGCHandle<LT>::V, LT, gcroot<typename UseT<::LK::IsGCHandle<LT>::V, System::Object^, LT>::T> >::T TT;
#else
		typedef LT TT;
#endif
		typedef typename UseT<IsBaseOf<ptr<Object>, TT>::V, TT, ptr<Object> >::T OBJT;
		//获取默认构造函数
		if (IsSame<TT, T>::V)
		{
			GetTypeCommonInfoRef()[index].size = sizeof(OBJT);
			GetTypeCommonInfoRef()[index].DeconstructMethod = &CommonTypeFunctionInfoStruct::template DefDeconstructMethod<OBJT>;
			GetTypeCommonInfoRef()[index].DefaultConstructMethod = &CommonTypeFunctionInfoStruct::template DefDefaultConstructMethod<OBJT>;
			GetTypeCommonInfoRef()[index].CopyConstructMethod = &CommonTypeFunctionInfoStruct::template DefCopyConstructMethod<OBJT>;
		}
		GetMutexRef().UnLock();
		return index;
	}
#ifdef SUPPORT_VARIADIC_TEMPLATES
	//抛出异常
	template<class ReturnType, class T, class ...Args>ReturnType RThrow(Args ...t) { throw T(t...); }
	template<class T, class ...Args>void Throw(Args ...t) { RThrow<void,T, Args...>(t...); }
#else
	//抛出异常
#define TMPF(x) template<MF4_RepeatAsc_(class T,,0,x)>void Throw(MF2_Repeat_b(MF1_Tt,x)){RThrow<void,MF4_RepeatAsc_(T,,0,x)>(MF4_RepeatAsc_(t,,1,x));}\
template<class RT,MF4_RepeatAsc_(class T,,0,x)>RT RThrow(MF2_Repeat_b(MF1_Tt,x)){throw T0(MF4_RepeatAsc_(t,,1,x));}
	MF3_RepeatAsc(TMPF, 0, 16);
#undef TMPF
#endif
	template<class _T, class _BT>struct __cast_tmp
	{
		static inline _T* _CastPointer(_BT const* p)
		{
			if (!p)return NULL;
			_T*pt = dynamic_cast<_T*>((_BT*)p);
			if (!pt)Throw< PPointerFailureException >();
			return pt;
		}
	};
	template<class _T>struct __cast_tmp<_T, _T> { static inline _T* _CastPointer(_T const* p) { return (_T*)p; } };
	//动态转换基类指针到派生类，不做检查
	template<class T, class BT>T* CastPointer(BT const* p)
	{
		return __cast_tmp<T, BT>::_CastPointer(p);
	}
	//静态转换派生类指针到基类，不做检查
	template<class T>Object* StaticCastPointer(T const* p) { return (Object*)p; }
	//检查指针p，并在p为NULL时抛出空指针异常
	template<class T, class BT>T* CheckPointer(BT const* p)
	{
		if (p)return CastPointer<T, BT>(p);
		return RThrow<T*, PPointerIsNullException >();
	}

	//因为需要抛出异常，模板实现需要移至此处
	template<class T>T* Memory::New(size_t len)
	{
		try
		{
			return new T[len];
		}
		catch (std::bad_alloc)
		{
			return RThrow<T*, ptr<OutOfMemoryException> >();
		}
	}
	template<class T>T* Memory::New(size_t Length, size_t* outSize)
	{
		size_t len = Memory::GetResize(Length*sizeof(T)) / sizeof(T);
		try
		{
			if (outSize)*outSize = len;
			return new T[len];
		}
		catch (std::bad_alloc)
		{
			return RThrow<T*, ptr<OutOfMemoryException> >();
		}
	}


#ifdef LK_MEMORYBUFFERS_
	inline void* Memory::GetDefaultBuffer()
	{
		static MemoryBuffer*p;
		if (p)return p;
		SpinMutex::GetDefaultRef().Lock();
		if (!p)
		{
			static MemoryBuffer m;
			p = &m;
		}
		SpinMutex::GetDefaultRef().UnLock();
		return p;
	}
#endif
	inline size_t Memory::GetBuffer(size_t count, unsigned char**p)
	{
		//每超过一定次数就清理一次缓存，减少缓存占用过多内存的问题
		static size_t i = 0;
		if (++i > 10000000) { i = 0;  ClearNotUsingBuffers(); }
#ifdef LK_MEMORYBUFFERS_
		return ((MemoryBuffer*)GetDefaultBuffer())->GetBuffer(count, p);
#else
		*p = new unsigned char[count];
		return 0;
#endif
	}
	inline void Memory::ReleaseBuffer(size_t mask, unsigned char*pointer)
	{
#ifdef LK_MEMORYBUFFERS_
		return ((MemoryBuffer*)GetDefaultBuffer())->ReleaseBuffer(mask, pointer);
#else
		delete[]pointer;
#endif
	}
	inline void Memory::ClearNotUsingBuffers()
	{
#ifdef LK_MEMORYBUFFERS_
		return ((MemoryBuffer*)GetDefaultBuffer())->ClearNotUsingBuffer();
#else

#endif
	}

	template <class T>ptr<String> Type::GetStringFromArgs()
	{
		typedef  typename UseT<IsSame<VarArgsPack<>, T>::V, T, VarArgsPack<void> >::T  TT;
		if (T::ArgCount == 0)return ptr<String>::GetEmpty();
		if (T::ArgCount == 1)return Make<typename ArgT<0, TT>::T>()->GetName();
		return Make<typename ArgT<0, TT>::T>()->GetName() + L"," + GetStringFromArgs<typename ArgRemoveAt<0, TT>::T>();
	}
#ifdef LK_FUNCTIONAL_
#ifdef SUPPORT_VARIADIC_TEMPLATES
	template<class ReturnType, class ...ArgumentsType>void  Functional::IFunObj<VarArgsPack<ReturnType, ArgumentsType...> >::AddTypeInfo()
	{
#ifdef LK_REFLECTION_
		//引用反射命名空间
		using namespace Reflection;
		//获取类库的引用
		ptr<TypeLibrary> lib = GetGlobleLibrary();
		//获取类库中的全局命名空间
		PNamespaceNode globle = lib.GetGlobleNamespaceRef();
		//添加LK命名空间
		PNamespaceNode space = globle.FindOrAddNamespace(L"LK");
		//在LK命名空间中添加ptr<Exception>类型
		PTypeNode n = space.FindOrAddTypeNode(L"IFunObj<" + ptr<String>(Type::Make<PFunObj<VarArgsPack<ReturnType, ArgumentsType...> > >()->GetID()) + L">", Type::Make<PFunObj<VarArgsPack<ReturnType, ArgumentsType...> > >());
		//设置继承
		n.AddBaseType(Type::Make<PFunObjs>());
#endif
	}
	template<class FunctionType, class ReturnType, class ...ArgumentsType>
	void Functional::FunObject<FunctionType, VarArgsPack<ReturnType, void, ArgumentsType...>, true>::AddTypeInfo()
	{
#ifdef LK_REFLECTION_
		//引用反射命名空间
		using namespace Reflection;
		//获取类库的引用
		ptr<TypeLibrary> lib = GetGlobleLibrary();
		//获取类库中的全局命名空间
		PNamespaceNode globle = lib.GetGlobleNamespaceRef();
		//添加LK命名空间
		PNamespaceNode space = globle.FindOrAddNamespace(L"LK");
		//在LK命名空间中添加ptr<Exception>类型
		PTypeNode n = space.FindOrAddTypeNode(L"FunObject<" + ptr<String>(Type::Make<PFunObject<FunctionType, VarArgsPack<ReturnType, void, ArgumentsType...>, true> >()->GetID()) + L">", Type::Make<PFunObject<FunctionType, VarArgsPack<ReturnType, void, ArgumentsType...>, true> >());
		//设置继承
		n.AddBaseType(Type::Make<PFunObj<VarArgsPack<ReturnType, ArgumentsType...> > >());
#endif
	}
	template<class FunctionType, class ReturnType, class CType, class ...ArgumentsType>
	void Functional::FunObject<FunctionType, VarArgsPack<ReturnType, CType, ArgumentsType...>, true>::AddTypeInfo()
	{
#ifdef LK_REFLECTION_
		//引用反射命名空间
		using namespace Reflection;
		//获取类库的引用
		ptr<TypeLibrary> lib = GetGlobleLibrary();
		//获取类库中的全局命名空间
		PNamespaceNode globle = lib.GetGlobleNamespaceRef();
		//添加LK命名空间
		PNamespaceNode space = globle.FindOrAddNamespace(L"LK");
		//在LK命名空间中添加ptr<Exception>类型
		PTypeNode n = space.FindOrAddTypeNode(L"FunObject<" + ptr<String>(Type::Make<PFunObject<FunctionType, VarArgsPack<ReturnType, CType, ArgumentsType...>, true> >()->GetID()) + L">", Type::Make<PFunObject<FunctionType, VarArgsPack<ReturnType, CType, ArgumentsType...>, true> >());
		//设置继承
		n.AddBaseType(Type::Make<PFunObj<VarArgsPack<ReturnType, ArgumentsType...> > >());
#endif
	}
#else
#define TMPCT(x) ,class T##x
#define TMPT(x) ,T##x
#ifdef LK_REFLECTION_
#define TMPF_1(x)\
		using namespace Reflection;\
		ptr<TypeLibrary> lib = GetGlobleLibrary();\
		PNamespaceNode globle = lib.GetGlobleNamespaceRef();\
		PNamespaceNode space = globle.FindOrAddNamespace(L"LK");\
		PTypeNode n = space.FindOrAddTypeNode(L"IFunObj<"\
		+ ptr<String>(Type::Make<PFunObj<VarArgsPack<ReturnType MF3_RepeatAsc(TMPT,2,x)> > >()->GetID()) \
		+ L">", Type::Make<PFunObj<VarArgsPack<ReturnType MF3_RepeatAsc(TMPT,2,x)> > >());\
		n.AddBaseType(Type::Make<PFunObjs>());
#define TMPF_2(x)\
		using namespace Reflection;\
		ptr<TypeLibrary> lib = GetGlobleLibrary();\
		PNamespaceNode globle = lib.GetGlobleNamespaceRef();\
		PNamespaceNode space = globle.FindOrAddNamespace(L"LK");\
		PTypeNode n = space.FindOrAddTypeNode(L"FunObject<"\
		+ ptr<String>(Type::Make<PFunObject<FunctionType, VarArgsPack<ReturnType, void MF3_RepeatAsc(TMPT,2,x)>, true> >()->GetID())\
		+ L">", Type::Make<PFunObject<FunctionType, VarArgsPack<ReturnType, void MF3_RepeatAsc(TMPT,2,x)>, true> >());\
		n.AddBaseType(Type::Make<PFunObj<VarArgsPack<ReturnType  MF3_RepeatAsc(TMPT,2,x)> > >());
#define TMPF_3(x)\
		using namespace Reflection;\
		ptr<TypeLibrary> lib = GetGlobleLibrary();\
		PNamespaceNode globle = lib.GetGlobleNamespaceRef();\
		PNamespaceNode space = globle.FindOrAddNamespace(L"LK");\
		PTypeNode n = space.FindOrAddTypeNode(L"FunObject<"\
		+ ptr<String>(Type::Make<PFunObject<FunctionType, VarArgsPack<ReturnType, CType MF3_RepeatAsc(TMPT,2,x)>, true> >()->GetID())\
		+L">",Type::Make<PFunObject<FunctionType, VarArgsPack<ReturnType, CType MF3_RepeatAsc(TMPT,2,x)>, true> >());\
		n.AddBaseType(Type::Make<PFunObj<VarArgsPack<ReturnType  MF3_RepeatAsc(TMPT,2,x)> > >());
#else
#define TMPF_1(x)
#define TMPF_2(x)
#define TMPF_3(x)
#endif
#define TMPF(x)\
	template<class ReturnType MF3_RepeatAsc(TMPCT,2,x)>void  Functional::IFunObj<VarArgsPack<ReturnType  MF3_RepeatAsc(TMPT,2,x)> >::AddTypeInfo(){TMPF_1(x)}\
	template<class FunctionType, class ReturnType MF3_RepeatAsc(TMPCT,2,x)>void Functional::FunObject<FunctionType, VarArgsPack<ReturnType, void MF3_RepeatAsc(TMPT,2,x)>, true>::AddTypeInfo(){TMPF_2(x)}\
	template<class FunctionType, class ReturnType,class CType MF3_RepeatAsc(TMPCT,2,x)>void Functional::FunObject<FunctionType, VarArgsPack<ReturnType, CType MF3_RepeatAsc(TMPT,2,x)>, true>::AddTypeInfo(){TMPF_3(x)}
	MF2_Repeatb(TMPF, 3)
#undef TMPF_1
#undef TMPF_2
#undef TMPF_3
#undef TMPF
#undef TMPCT
#undef TMPT
#endif
#ifdef LK_FUNCTIONAL_ACTION_
#ifdef SUPPORT_VARIADIC_TEMPLATES
		template<class T0, class ...Args>ptr <Functional::IActions>::ptr(Functional::PDelegates d, T0 t0, Args ... t)
		:Functional::PFunObject<weakptr<Functional::IActions>, VarArgsPack<void, void>, true>(new Functional::IActions())
	{
		if (d==null)Throw< PInvalidArgumentException>(L"指定的委托为空");
		GetPtr()->delegates = d;
		GetPtr()->args = new Collection::DefaultArray<ptr<Object>>();
		AddArgs<T0, Args...>(t0, t...);
	}
	template<class T0, class ...Args>ptr<Functional::IActions> ptr<Functional::IActions>::AddArgs(T0 t0, Args ... t)
	{
		GetPtr()->args->Append(MObjects::MakeObject<T0>(t0));
		return AddArgs<Args...>(t...);
	}
#else
#define TMPCT(x) ,class T##x
#define TMPTt(x) ,T##x t##x
#define TMPAPPENDt(x) GetPtr()->args.Append(Object::Make<T##x>(t##x));
#define TMPF(x)\
		template<class T1 MF3_RepeatAsc(TMPCT,2,x)>ptr<Functional::IActions>::ptr(Functional::PDelegates d MF3_RepeatAsc(TMPTt,1,x))\
		:Functional::PFunObject<weakptr<Functional::IActions>, VarArgsPack<void, void>, true>(new Functional::IActions()){\
			if (d==null)Throw< PInvalidArgumentException>(L"指定的委托为空");\
			GetPtr()->delegates = d;\
			GetPtr()->args = new Collection::DefaultArray<ptr<Object>>();\
			AddArgs<MF4_RepeatAsc_(T,,1,x)>(MF4_RepeatAsc_(t,,1,x));\
		}\
		template<class T0 MF3_RepeatAsc(TMPCT,1,x)> ptr<Functional::IActions> ptr<Functional::IActions>::AddArgs(ptr<String> const&functionName MF3_RepeatAsc(TMPTt,0,x))const\
		{\
			MF3_RepeatAsc(TMPAPPENDt,0,x)\
			return*this;\
		}
		MF2_Repeatb(TMPF, _MRP_CNTM1)
#undef TMPF
#undef TMPAPPENDt
#undef TMPCT
#undef TMPT
#endif
		template<class T0>ptr<Functional::IActions> ptr<Functional::IActions>::AddArgs(T0 t0)
	{
		GetPtr()->args->Append(MObjects::MakeObject<T0>(t0));
		return *this;
	}
#endif
#endif
	template<class T>void Collection::IArray<T> ::AddTypeInfo()
	{
#ifdef LK_REFLECTION_
		//引用反射命名空间
		using namespace Reflection;
		//获取类库的引用
		ptr<TypeLibrary> lib = GetGlobleLibrary();
		//获取类库中的全局命名空间
		PNamespaceNode globle = lib.GetGlobleNamespaceRef();
		//添加LK命名空间
		PNamespaceNode space = globle.FindOrAddNamespace(L"LK");
		space = space.FindOrAddNamespace(L"Collection");
		//在LK命名空间中添加PArray<T> 类型
		PTypeNode n = space.FindOrAddTypeNode(L"IArray<" + Type::Make<T>()->ToString() + L">", Type::Make<ptr<IArray<T> > >());
		//设置继承关系
		n.AddBaseType(Type::Make<ptr<ICloneables>>());
		n.AddBaseType(Type::Make<ptr<ICollections>>());
		n.AddBaseType(Type::Make<ptr<ISortables> >());
#endif
	}
	template<class T>void Collection::DefaultArray<T>::AddTypeInfo()
	{
#ifdef LK_REFLECTION_
		//引用反射命名空间
		using namespace Reflection;
		//获取类库的引用
		ptr<TypeLibrary> lib = GetGlobleLibrary();
		//获取类库中的全局命名空间
		PNamespaceNode globle = lib.GetGlobleNamespaceRef();
		//添加LK命名空间
		PNamespaceNode space = globle.FindOrAddNamespace(L"LK");
		space = space.FindOrAddNamespace(L"Collection");
		//在LK命名空间中添加PDefaultArray<T>类型
		PTypeNode n = space.FindOrAddTypeNode(L"DefaultArray<" + Type::Make<T>()->ToString() + L">", Type::Make<ptr<DefaultArray<T>> >());
		//设置继承
		n.AddBaseType(Type::Make<ptr<IArray<T> > >());
#endif
	}
	template<class T>void Collection::Vector<T>::AddTypeInfo()
	{
#ifdef LK_REFLECTION_
		//引用反射命名空间
		using namespace Reflection;
		//获取类库的引用
		ptr<TypeLibrary> lib = GetGlobleLibrary();
		//获取类库中的全局命名空间
		PNamespaceNode globle = lib.GetGlobleNamespaceRef();
		//添加LK命名空间
		PNamespaceNode space = globle.FindOrAddNamespace(L"LK");
		space = space.FindOrAddNamespace(L"Collection");
		//在LK命名空间中添加PVector<T>类型
		PTypeNode n = space.FindOrAddTypeNode(L"Vector<" + Type::Make<T>()->ToString() + L">", Type::Make<ptr<Vector<T> >>());
		//设置继承
		n.AddBaseType(Type::Make<ptr<Vector<T> >>());
#endif
	}

}
